package dialog;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import file.FileManagement;
import panel.Box;
import screen.Main;

@SuppressWarnings("serial")
public abstract class Dialog extends JFrame implements MouseListener, MouseMotionListener{
	
	/*window*/
	Main main;
	
	/*Panel*/
	Box box;
	
	/*mouse*/
	int px, py, rx, ry, mx, my, dx, dy;
	boolean isDrag;

	/*size*/
	private int height;
	
	/*file*/
	FileManagement fm;
	
	/*clean*/
	boolean canClean = true;			
	
	public Dialog(Main main, int height, FileManagement fm){
		this.main = main;
		this.height = height;
		this.fm = fm;
		
		/*control screen*/
		main.setEnabled(false);
		
		/* Panel */
		box = new Box();
		setContentPane(box);

		/* Frame */
		setSize(400, height);
		setUndecorated(true);
		setLocationRelativeTo(null);
		setVisible(true);
		setLayout(null);
		
		/* Listener */
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		px = e.getX();
		py = e.getY();

		/* exit & drag */
		if (px >= 377 && px <= 393 && py >= 3 && py <= 19){
			main.setEnabled(true);
			dispose();
		}
		else if (py <= 23)
			isDrag = true;

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		if (isDrag)
			isDrag = false;
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		if (isDrag) {
			dx = e.getXOnScreen();
			dy = e.getYOnScreen();

			setLocation(dx - px, dy - py);
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		mx = e.getX();
		my = e.getY();

		/* exit & iconified */
		if (mx >= 372 && mx <= 398 && my >= 0 && my <= 24) {
			if (mx >= 377 && mx <= 393 && my >= 3 && my <= 19) {
				box.setStateButtonExit(1);
				
				canClean = true;
				box.repaint();
			}
		} 
		/*clean*/
		else if(canClean){
			box.setStateButtonExit(0);
			
			canClean = false;
			box.repaint();
		}
	}
}